package xkp.lesson.controller;


import com.sun.deploy.net.HttpResponse;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xkp.lesson.entity.Dept;
import xkp.lesson.entity.Emp;
import xkp.lesson.entity.Point;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/hello")
public class HelloController {
    @GetMapping("/addEmp.action")
    public void addEmp(Integer empno, String ename, String job, Double sal, Dept dept, Date hiredate, String[] hobbies, HttpServletResponse response) throws IOException {
        System.out.println(empno);
        System.out.println(ename);
        System.out.println(job);
        System.out.println(sal);
        System.out.println(hiredate);
        Emp emp = new Emp();
        //emp.getEmpno();
        response.getWriter().write("hello");
    }

    @PostMapping("/addEmp.action")
    public void addEmp2(@RequestParam("empno") Integer empno, String ename, String job, Double sal, Date hiredate, HttpServletResponse response) throws IOException {
        System.out.println(empno);
        System.out.println(ename);
        System.out.println(job);
        System.out.println(sal);
        System.out.println(hiredate);
        response.getWriter().write("hello");
    }


    @PostMapping("/addEmp3.action")
    public void addEmp3(Emp emp, HttpServletResponse response) throws IOException {
        System.out.println(emp);
        response.getWriter().write("hello");
    }

    @RequestMapping(value = "/dateTimeFormat.action",method = RequestMethod.GET)
    public String dateTimeFormat(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        System.out.println("日期为:" + date);
        return "redirect:/index.jsp";
    }

    @RequestMapping("/requestHeader.action")
    public String requestHeader(@RequestHeader("Accept-Language") String language){
        System.out.println("请求头信息:" + language);
        return "redirect:/index.jsp";
    }

    @RequestMapping("/setCookie.action")
    public String setCookie(HttpServletResponse response){
        System.out.println("设置Cookie...");
        Cookie myCookie = new Cookie("username", "xiaoming");
        myCookie.setPath("/hello/setCookie.action");
        //myCookie.setPath("/");
        myCookie.setMaxAge(60 * 60 * 24);
        response.addCookie(myCookie);
        return "redirect:/index.jsp";
    }

    @RequestMapping("/getCookie.action")
    public String getCookie(@CookieValue("username") String username){
        System.out.println("Cookie:" + username);
        return "redirect:/index.jsp";
    }

    @RequestMapping("/setSession.action")
    public String setSessionInfo(HttpSession session){
        Emp emp = new Emp();
        emp.setEname("张三");
        session.setAttribute("emp",emp);
        return "redirect:/index.jsp";
    }

    @RequestMapping("/getSession.action")
    public String getSessionInfo(@SessionAttribute("emp") Emp emp){
        System.out.println(emp);
        return "redirect:/index.jsp";
    }

    @PostMapping("/getPoint.action")
    public String getPoint(Point point){
        System.out.println(point);
        return "redirect:/index.jsp";
    }
}